/**
 * 邮箱激活
 * @param $rootScope
 * @param $scope
 * @param $state
 * @param registerrightService
 */
module.exports = function ($rootScope, $scope, $state, registerrightService) {
    /*5秒倒计时*/
    var wait = 5;
    /*接口成功标志：success 为成功*/
    $scope.flag = "";
    /**
     * 初始化
     */
    $scope.init = function () {
      // alert("1111");
        $scope.emailActive();
    }

    /**
     * 5秒倒计时方法
     */
    $scope.time = function () {
        if (wait == 0) {
            $state.go('loginHeader.login');
        } else {
            $('.register-right-return').html(wait);
            wait--;
            setTimeout(function () {
                $scope.time()
            }, 1000)
        }
    }

    /**
     * 点击确定按钮方法
     */
    $('.email-go-verification').click(function () {
        if ($scope.flag == "success") {
            $state.go('loginHeader.login');
        } else {
            alert("抱歉！激活邮箱出错，请关闭浏览器重试。");
        }
    });


    /**
     * 获取Url中邮箱参数
     * @returns {*}
     * @constructor
     */
    function GetRequest() {
        var url = window.location.href; //获取url中"?"符后的字串
        if (url.indexOf("?") != -1) {
            var str = url.split('?');
            var param = str[1].split('=');
            var value = param[1];
        }
        return value;
    }

    /**
     * 激活邮箱接口
     */
    $scope.emailActive = function () {
      // alert("emailActive");
        /*获取邮箱*/
        var emailValue = GetRequest();
// alert(emailValue);
        if (emailValue == null || emailValue == undefined || emailValue == "") {
            alert("抱歉！激活邮箱出错，请关闭浏览器重试。");
        } else {
            registerrightService.emailActive(emailValue).then(function (data) {
                if (data.code == 1) {
                    $scope.time();
                    $scope.flag = "success";
                }
                // else {
                //   alert("email:" + emailValue);
                //     $state.go('loginHeader.registerfail', {email:emailValue});
                // }
            }, function (response) {
                console.error('emailActive error', response.status, response.statusText);
            });
        }
    };

    $scope.init();
};
